/**
 * Created by mac on 2017/6/7.
 */
import static java.lang.System.out;
import java.sql.*;

public class Connection02 {
    public static void main(String[] args)
            throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String jdbcUrl = "jdbc:mysql://localhost:3306/world";
        String user = "root";
        String passwd = "";
        try (Connection conn =
                     DriverManager.getConnection(jdbcUrl, user, passwd)) {
            out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");

            Statement stmt = conn.createStatement(); //创建Statement对象
            String sql = "SELECT sum(Population) FROM `city` WHERE District= 'New Jersey'";    //要执行的SQL
            ResultSet rs = stmt.executeQuery(sql);//创建数据对象
            //System.out.println("ID" + "\t"+"\t" + "Name" +"\t"+ "\t" + "CountryCode"+ "\t"+"\t" + "Distridt" + "\t"+"\t" + "Population");
            //int count = 0;
            /*while (rs.next()) {
                System.out.print(rs.getInt(1) + "\t"+"\t");
                System.out.print(rs.getString(2) + "\t"+"\t");
                System.out.print(rs.getString(3) + "\t"+"\t");
                System.out.print(rs.getString(4) + "\t"+"\t");
                System.out.print(rs.getString(5) + "\t"+"\t");
                System.out.println();
                count++;
            }*/
            if(rs.next());
                System.out.println(rs.getString(1));
            //System.out.printf("There are all %d answers",count);
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
